# Replication codes for mediation and sensitivity analyses in "Propaganda, Presumed Influence, and Collective Protest" by Haifeng Huang and Nicholas Cruz, Political Behavior.
# Prepared in December 2020

library(haven)
data1 <- read_dta("propaganda_protest2020.dta")
library(mediation)
library(sandwich)
library(lmtest)
library(car)

#---------------------------------------------------------------------------------------------------------------------------------
# Codes for Table 4:
#---------------------------------------------------------------------------------------------------------------------------------
## Propaganda's Effect on Self Protest with Other Protest as the Mediator
### Video Treatment

data1.v <- data1
data1.v$vc <- ifelse(data1.v$video == 1, 1, ifelse(data1.v$control == 1, 1, 0))
data1.v <- data1.v[data1.v$vc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.v <- lm(protestother_scale ~ 
                          video +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity,
                        data = data1.v)
summary(protest.med.fit.v)
#### Regress Treatment on Outcome
protest.out.fit.v <- lm(protestself_scale ~ 
                          video +
                          protestother_scale +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity, 
                        data = data1.v)
summary(protest.out.fit.v)
#### Mediation Model
protest.med.out.v <- mediate(protest.med.fit.v, protest.out.fit.v, treat = "video", mediator = "protestother_scale", robustSE = FALSE, sims = 10000)
summary(protest.med.out.v)
#### ACME Standard Error
(-0.06515--0.03590)/-1.96
#### ADE Standard Error
(-0.01054-0.02915)/-1.96
#### Total Effect Standard Error
(-0.05471--0.00675)/-1.96


### Democracy Treatment
data1.d <- data1
data1.d$dc <- ifelse(data1.d$democracy == 1, 1, ifelse(data1.d$control == 1, 1, 0))
data1.d <- data1.d[data1.d$dc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.d <- lm(protestother_scale ~ 
                          democracy +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity,
                        data = data1.d)
summary(protest.med.fit.d)
#### Regress Treatment on Outcome
protest.out.fit.d <- lm(protestself_scale ~ 
                          democracy +
                          protestother_scale +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity, 
                        data = data1.d)
summary(protest.out.fit.d)
#### Mediation Model
protest.med.out.d <- mediate(protest.med.fit.d, protest.out.fit.d, treat = "democracy", mediator = "protestother_scale", robustSE = TRUE, sims = 10000)
summary(protest.med.out.d)
#### ACME Standard Error
(-0.05776--0.03038)/-1.96
#### ADE Standard Error
(-0.03583-0.00845)/-1.96
#### Total Effect Standard Error
(-0.07195--0.02193)/-1.96

### Book Treatment
data1.b <- data1
data1.b$bc <- ifelse(data1.b$book == 1, 1, ifelse(data1.b$control == 1, 1, 0))
data1.b <- data1.b[data1.b$bc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.b <- lm(protestother_scale ~ 
                          book +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity,
                        data = data1.b)
summary(protest.med.fit.b)
#### Regress Treatment on Outcome
protest.out.fit.b <- lm(protestself_scale ~ 
                          book +
                          protestother_scale +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity, 
                        data = data1.b)
summary(protest.out.fit.b)
#### Mediation Model
protest.med.out.b <- mediate(protest.med.fit.b, protest.out.fit.b, treat = "book", mediator = "protestother_scale", robustSE = TRUE, sims = 10000)
summary(protest.med.out.b)
#### ACME Stabdard Error
(-0.0567--0.0000)/-1.96
#### ADE Standard Error
(0.000742-0.0416)/-1.96
#### Total Effect Standard Error
(-0.0366-0.0129)/-1.96

### Space Treatment
data1.s <- data1
data1.s$sc <- ifelse(data1.s$space == 1, 1, ifelse(data1.s$control == 1, 1, 0))
data1.s <- data1.s[data1.s$sc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.s <- lm(protestother_scale ~ 
                          space +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity,
                        data = data1.s)
summary(protest.med.fit.s)
#### Regress Treatment on Outcome
protest.out.fit.s <- lm(protestself_scale ~ 
                          space +
                          protestother_scale +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity, 
                        data = data1.s)
summary(protest.out.fit.s)
#### Mediation Model
protest.med.out.s <- mediate(protest.med.fit.s, protest.out.fit.s, treat = "space", mediator = "protestother_scale", robustSE = TRUE, sims = 10000)
summary(protest.med.out.s)
#### ACME Standard Error
(-0.0590--0.0305)/-1.96
#### ADE Standard Error
(0.0167-0.0605)/-1.96
#### Total Effect Standard Error
(-0.0210-0.0301)/-1.96

### Binary Treatment
data1.t <- data1
data1.t$tc <- ifelse(data1.t$treated == 1, 1, ifelse(data1.t$control == 1, 1, 0))
data1.t <- data1.t[data1.t$tc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.t <- lm(protestother_scale ~ 
                          treated +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity,
                        data = data1.t)
summary(protest.med.fit.t)
#### Regress Treatment on Outcome
protest.out.fit.t <- lm(protestself_scale ~ 
                          treated +
                          protestother_scale +
                          female +
                          age +
                          education +
                          income +
                          ccpmember +
                          urban +
                          life +
                          pinterest +
                          pride +
                          prowest +
                          polity, 
                        data = data1.t)
summary(protest.out.fit.t)
#### Mediation Model
protest.med.out.t <- mediate(protest.med.fit.t, protest.out.fit.t, treat = "treated", mediator = "protestother_scale", robustSE = TRUE, sims = 10000)
summary(protest.med.out.t)
#### ACME Standard Error
(-0.05515--0.03235)/-1.96
#### ADE Standard Error
(0.00042-0.03426)/-1.96
#### Total Effect Standard Error
(-0.03881-0.00191)/-1.96

#---------------------------------------------------------------------------------------------------------------------------------
# Codes for Figure 5:
#---------------------------------------------------------------------------------------------------------------------------------
### Video Treatment Sensitivity Analysis
protest.med.out.v.sens <- medsens(protest.med.out.v, rho.by = 0.1, 
                                  effect.type = "indirect", sims = 100)
summary(protest.med.out.v.sens)
plot(protest.med.out.v.sens, sens.par = "rho", main = "Protest Mediation Sensitivity: Video")

### Democracy Treatment Sensitivity Analysis
protest.med.out.d.sens <- medsens(protest.med.out.d, rho.by = 0.1, 
                                  effect.type = "indirect", sims = 100)
summary(protest.med.out.d.sens)
plot(protest.med.out.d.sens, sens.par = "rho", main = "Protest Mediation Sensitivity: Democracy")

### Book Treatment Sensitivity Analysis
protest.med.out.b.sens <- medsens(protest.med.out.b, rho.by = 0.1, 
                                  effect.type = "indirect", sims = 100)
summary(protest.med.out.b.sens)
plot(protest.med.out.b.sens, sens.par = "rho", main = "Protest Mediation Sensitivity: Book")

### Space Treatment Sensitivity Analysis
protest.med.out.s.sens <- medsens(protest.med.out.s, rho.by = 0.1, 
                                  effect.type = "indirect", sims = 100)
summary(protest.med.out.s.sens)
plot(protest.med.out.s.sens, sens.par = "rho", main = "Protest Mediation Sensitivity: Space")

### Binary Treatment Sensitivity Analysis
protest.med.out.t.sens <- medsens(protest.med.out.t, rho.by = 0.1, 
                                  effect.type = "indirect", sims = 100)
summary(protest.med.out.t.sens)
plot(protest.med.out.t.sens, sens.par = "rho", main = "Protest Mediation Sensitivity: Treated")

#---------------------------------------------------------------------------------------------------------------------------------
# Codes for Table 5:
#---------------------------------------------------------------------------------------------------------------------------------
## Propaganda's Effect on Other Protest with Self Protest as the Mediator
### Video Treatment
data1.v <- data1
data1.v$vc <- ifelse(data1.v$video == 1, 1, ifelse(data1.v$control == 1, 1, 0))
data1.v <- data1.v[data1.v$vc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.v1 <- lm(protestself_scale ~ 
                           video +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity,
                         data = data1.v)
summary(protest.med.fit.v1)
#### Regress Treatment on Outcome
protest.out.fit.v1 <- lm(protestother_scale ~ 
                           video +
                           protestself_scale +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity, 
                         data = data1.v)
summary(protest.out.fit.v1)
#### Mediation Model
protest.med.out.v1 <- mediate(protest.med.fit.v1, protest.out.fit.v1, treat = "video", mediator = "protestself_scale", robustSE = FALSE, sims = 10000)
summary(protest.med.out.v1)
#### ACME Standard Error
(-0.0372--0.003193)/-1.96
#### ADE Standard Error
(-0.09763--0.05793)/-1.96
#### Total Effect Standard Error
(-0.11005--0.06165)/-1.96

### Democracy Treatment
data1.d <- data1
data1.d$dc <- ifelse(data1.d$democracy == 1, 1, ifelse(data1.d$control == 1, 1, 0))
data1.d <- data1.d[data1.d$dc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.d1 <- lm(protestself_scale ~ 
                           democracy +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity,
                         data = data1.d)
summary(protest.med.fit.d1)
#### Regress Treatment on Outcome
protest.out.fit.d1 <- lm(protestother_scale ~ 
                           democracy +
                           protestself_scale +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity, 
                         data = data1.d)
summary(protest.out.fit.d1)
#### Mediation Model
protest.med.out.d1 <- mediate(protest.med.fit.d1, protest.out.fit.d1, treat = "democracy", mediator = "protestself_scale", robustSE = TRUE, sims = 10000)
summary(protest.med.out.d1)
#### ACME Standard Error
(-0.0389--0.0119)/-1.96
#### ADE Standard Error
(-0.0857--0.0443)/-1.96
#### Total Effect Standard Error
(-0.1058--0.0562)/-1.96

### Book Treatment
data1.b <- data1
data1.b$bc <- ifelse(data1.b$book == 1, 1, ifelse(data1.b$control == 1, 1, 0))
data1.b <- data1.b[data1.b$bc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.b1 <- lm(protestself_scale ~ 
                           book +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity,
                         data = data1.b)
summary(protest.med.fit.b1)
#### Regress Treatment on Outcome
protest.out.fit.b1 <- lm(protestother_scale ~ 
                           book +
                           protestself_scale +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity, 
                         data = data1.b)
summary(protest.out.fit.b1)
#### Mediation Model
protest.med.out.b1 <- mediate(protest.med.fit.b1, protest.out.fit.b1, treat = "book", mediator = "protestself_scale", robustSE = TRUE, sims = 10000)
summary(protest.med.out.b1)
#### ACME Standard Error
(-0.01873-0.00664)/-1.96
#### ADE Standard Error
(-0.09744--0.05769)/-1.96
#### Total Effect Standard Error
(-0.09889--0.05105)/-1.96

### Space Treatment
data1.s <- data1
data1.s$sc <- ifelse(data1.s$space == 1, 1, ifelse(data1.s$control == 1, 1, 0))
data1.s <- data1.s[data1.s$sc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.s1 <- lm(protestself_scale ~ 
                           space +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity,
                         data = data1.s)
summary(protest.med.fit.s1)
#### Regress Treatment on Outcome
protest.out.fit.s1 <- lm(protestother_scale ~ 
                           space +
                           protestself_scale +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity, 
                         data = data1.s)
summary(protest.out.fit.s1)
#### Mediation Model
protest.med.out.s1 <- mediate(protest.med.fit.s1, protest.out.fit.s1, treat = "space", mediator = "protestself_scale", robustSE = TRUE, sims = 10000)
summary(protest.med.out.s1)
#### ACME Standard Error
(-0.0113-0.0161)/-1.96
#### ADE Standard Error
(-0.1152--0.0727)/-1.96
#### Total Effect Standard Error
(-0.1076--0.0566)/-1.96

### Binary Treatment
data1.t <- data1
data1.t$tc <- ifelse(data1.t$treated == 1, 1, ifelse(data1.t$control == 1, 1, 0))
data1.t <- data1.t[data1.t$tc == 1, ]
#### Regress Treatment on Mediator
protest.med.fit.t1 <- lm(protestself_scale ~ 
                           treated +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity,
                         data = data1.t)
summary(protest.med.fit.t1)
#### Regress Treatment on Outcome
protest.out.fit.t1 <- lm(protestother_scale ~ 
                           treated +
                           protestself_scale +
                           female +
                           age +
                           education +
                           income +
                           ccpmember +
                           urban +
                           life +
                           pinterest +
                           pride +
                           prowest +
                           polity, 
                         data = data1.t)
summary(protest.out.fit.t1)
#### Mediation Model
protest.med.out.t1 <- mediate(protest.med.fit.t1, protest.out.fit.t1, treat = "treated", mediator = "protestself_scale", robustSE = TRUE, sims = 10000)
summary(protest.med.out.t1)
#### ACME Standard Error
(-0.0211-0.0012)/-1.96
#### ADE Standard Error
(-0.0893--0.0564)/-1.96
#### Total Effect Standard Error
(-0.0954--0.0552)/-1.96